#coding:utf-8

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cross_validation import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import metrics
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import Ridge

df = pd.read_csv('E:\\death rate.csv')
df = df.dropna()
df = df[0 < df.q_male]
mydata = df[df.q_male <= 1]

#1
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.grid(color='#95a5a6',linestyle='--', linewidth=3,axis='both',alpha=0.4)

plt.scatter(mydata['Age'],np.log(mydata['q_male']))
plt.title(u'年龄与男性的死亡率（对数）的关系')
plt.xlabel('df$Age')
plt.ylabel('log(df$q_male)')
plt.gcf().savefig(u'E:\\年龄与男性的死亡率（对数）的关系.png')
plt.show()
# #2
plt.scatter(mydata['Year'],np.log(mydata['q_male']))
plt.title(u'年份与男性的死亡率（对数）的关系')
plt.xlabel('df$Year')
plt.ylabel('log(df$q_male)')
plt.gcf().savefig(u'E:\\年份与男性的死亡率（对数）的关系.png')
plt.show()
# #3
plt.scatter(mydata['Age'],mydata['L_male_exp'])
plt.title(u'年龄与男性对数生存人数')
plt.xlabel('df$Age')
plt.ylabel('df$L_male_exp')
plt.gcf().savefig(u'E:\\年龄与男性对数生存人数的关系.png')
plt.show()
# #4
plt.hist(mydata.Male_death,50)
plt.title('histogram of mydata$Male_death')
plt.xlabel('mydata$Male_death')
plt.ylabel('Density')
plt.gcf().savefig('E:\\histogram of mydata$Male_death.png')
plt.show()
#5
plt.hist(np.log(mydata.Male_death),50)
plt.title('histogram of mydata$Male_death')
plt.xlabel('log(mydata.Male_death)')
plt.ylabel('Density')
plt.gcf().savefig('E:\\histogram of log(mydata.Male_death).png')
plt.show()

mydata1 = mydata[['Age','Year','L_male_exp','Male_death']]

train, test = train_test_split(mydata1,test_size=0.25,random_state=0)

Xi_train=train[['Age','Year','L_male_exp']]
Yi_train=train['Male_death']

Xi_test=test[['Age','Year','L_male_exp']]
Yi_test=test['Male_death']


##设置模型
model = LinearRegression()
##训练数据
model.fit(Xi_train,Yi_train)


##用训练得出的模型预测数据
y_plot = model.predict(Xi_test)

##绘图
plt.scatter(Yi_test,y_plot)
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.title(u'最小二乘法拟合：观测值与拟合值')
plt.xlabel(u'观测值')
plt.ylabel(u'拟合值')
plt.gcf().savefig('E:\\Least squares method.png')
plt.show()

print "Least squares method test R^2:",model.score(Xi_test,Yi_test)
print "Least squares method train R^2:",model.score(Xi_train,Yi_train)
print "Least squares method MSE:",metrics.mean_squared_error(Yi_test,y_plot)

#多项式回归
#这里指定使用岭回归作为基函数
model = make_pipeline(PolynomialFeatures(9), Ridge(alpha=10,solver='auto'))
model.fit(Xi_train,Yi_train)
##根据模型预测结果
y_plot = model.predict(Xi_test)


##绘图
plt.scatter(Yi_test,y_plot)
plt.title(u'多项式拟合：观测值与拟合值')
plt.xlabel(u'观测值')
plt.ylabel(u'拟合值')
plt.gcf().savefig('E:\\Polynomial.png')
plt.show()
print "Polynomial test R^2:",model.score(Xi_test,Yi_test)
print "Polynomial MSE:",metrics.mean_squared_error(Yi_test,y_plot)